package lanQiaoBei.动态规划.线性dp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//https://www.likecs.com/show-307180983.html ——保存最长上升子序列并打印
public class 最长上升子序列加保存子序列 {
	   static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	   final static int N=1010;
	   static int f[]=new int[N],a[]=new int[N],st[]=new int[N],n;
	   
	   public static void main(String[] ss) throws IOException {
		      n=Integer.parseInt(br.readLine());
		      ss=br.readLine().split(" ");
		      for(int i=0;i<n;i++)
		    	  a[i]=Integer.parseInt(ss[i]);
		      
		      for(int i=0;i<n;i++){
		    	  f[i]=1;
		    	  st[i]=-1;
		    	  for(int j=0;j<i;j++)
		    		  if(a[i]>a[j]&&f[j]+1>f[i]){
		    			  f[i]=f[j]+1;
		    			  st[i]=j;
		    		  }
		      }
		      int max=-0x3f3f3f3f,k=0;
		      for(int i=0;i<n;i++)
		    	  if(f[i]>max){
		    		  max=f[i];
		    		  k=i;
		    	  }
		      System.out.println(max);
		      while(k!=-1){
		    	   System.out.print(a[k]+" ");
		    	   k=st[k];
		      }
	}
       
}
